5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup Maintenance
27 use MediaWiki\Shell\Shell;
30 * Stream wrapper around 7za filter program.
31 * Required since we can't pass an open file resource to XMLReader->open()
32 * which is used for the text prefetch.
34 * @ingroup Maintenance
36 class SevenZipStream {
39 private function stripPath( $path ) {
40 $prefix = 'mediawiki.compress.7z://';
42 return substr( $path, strlen( $prefix ) );
45 function stream_open( $path, $mode, $options, &$opened_path ) {
46 if ( $mode[0] == 'r' ) {
47 $options = 'e -bd -so';
48 } elseif ( $mode[0] == 'w' ) {
49 $options = 'a -bd -si';
53 $arg = Shell::escape( $this->stripPath( $path ) );
54 $command = "7za $options $arg";
55 if ( !wfIsWindows() ) {
56 // Suppress the stupid messages on stderr
57 $command .= ' 2>/dev/null';
59 $this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
60 return ( $this->stream !== false );
63 function url_stat( $path, $flags ) {
64 return stat( $this->stripPath( $path ) );
67 // This is all so lame; there should be a default class we can extend
69 function stream_close() {
70 return fclose( $this->stream );
73 function stream_flush() {
74 return fflush( $this->stream );
77 function stream_read( $count ) {
78 return fread( $this->stream, $count );
81 function stream_write( $data ) {
82 return fwrite( $this->stream, $data );
85 function stream_tell() {
86 return ftell( $this->stream );
89 function stream_eof() {
90 return feof( $this->stream );
93 function stream_seek( $offset, $whence ) {
94 return fseek( $this->stream, $offset, $whence );
98 stream_wrapper_register( 'mediawiki.compress.7z', SevenZipStream::class );